//导入axios  npm install axios
import axios from 'axios';
//定义一个变量,记录公共的前缀  ,  baseURL
const baseURL = 'http://47.109.106.81:8081/';
const instance = axios.create({baseURL})

import { useTokenStore,Store } from '@/pages/stores/token.js'
// 添加请求拦截器
instance.interceptors.request.use(
    (config) => {
        //请求前的回调
        //添加token
        const tokenStore = useTokenStore();
        //判断有没有token
        // if (tokenStore.token) {
		if(Store.getToken()){
            // config.headers.Authorization = tokenStore.token
			config.headers.Authorization=Store.getToken()
        }
        return config;
    },
    (err) => {
        //请求错误的回调
        Promise.reject(err)
    }
)


//添加响应拦截器
instance.interceptors.response.use(
    result => {
		// console.log(result)
        if (result.data.code === 1) {
            return result.data;
        }
        // alert(result.data.msg ? result.data.msg : '服务异常1')
        return Promise.reject(result.data)
    },
    err => {
        console.log(err)
        if (err.response.status === 401) {
            // ElMessage.error('请先登录')
            uni.redirectTo({
				url:'/pages/views/login'
			})

        }else{
            // alert('服务异常2')
        }
        return Promise.reject(err)
    }
)
export default instance;